//
//  ChangeNickNameViewController.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/22.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "ChangeNickNameViewController.h"
#import "LoginPwdChangeCell.h"
#import "UserViewModel.h"

@interface ChangeNickNameViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UserViewModel *viewModel;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *subMitBtn;
@property (nonatomic, strong) UIView *cellRootView;
@property (nonatomic, strong) UITextField *cellTextField;

@end

@implementation ChangeNickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = kLocalizedString(@"基本资料修改", nil);

    [self.view addSubview:self.tableView];
    [self.tableView addSubview:self.subMitBtn];
    
    [self outPutAction];
    
    UITapGestureRecognizer *tableViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewTouchInSide)];
    tableViewGesture.numberOfTapsRequired = 1;
    tableViewGesture.cancelsTouchesInView = NO;
    [self.tableView addGestureRecognizer:tableViewGesture];
}

- (void)tableViewTouchInSide
{
    [self.view endEditing:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *iden = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:iden];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _cellRootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenNarrowSide, 60)];
        _cellRootView.backgroundColor = [UIColor whiteColor];
        [cell.contentView addSubview:_cellRootView];
        
        _cellTextField = [[UITextField alloc]initWithFrame:CGRectMake(15, 0, _cellRootView.width - 30, 60)];
        _cellTextField.placeholder = kLocalizedString(@"点击此处修改昵称", nil);
        _cellTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _cellTextField.font = [UIFont systemFontOfSize:15];
        _cellTextField.textColor = DefaultBlueColor;
        [_cellRootView addSubview:_cellTextField];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

#pragma mark - Input

- (void)subMitAction:(UIButton *)btn
{
    [self.view endEditing:YES];
    [self startClearBgColorLoading];
    [self.viewModel.changeUserNameCommand execute:@{@"ID":[CBTLocalUserManager sharedInstance].userId,@"name":self.cellTextField.text}];
}

#pragma mark - OutPut

- (void)outPutAction
{
    @weak(self)
    //修改昵称成功
    [self.viewModel.changeUserNameCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"changeUserInfo" object:nil userInfo:@{@"type":@"userName"}];
        [self_weak_ stopLoading];
        [self_weak_.navigationController popViewControllerAnimated:YES];
    }];
    
    //修改昵称失败
    [self.viewModel.changeUserNameCommand.errors subscribeNext:^(NSError *x) {
        [self_weak_ stopLoading];
    }];
}

#pragma mark - Lazy

- (UserViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[UserViewModel alloc]init];
    }
    return _viewModel;
}

- (UIButton *)subMitBtn
{
    if (!_subMitBtn) {
        _subMitBtn = [[UIButton alloc]initWithFrame:CGRectMake(25, 142, ScreenNarrowSide - 50, 40)];
        [_subMitBtn setTitle:kLocalizedString(@"确定", nil) forState:(UIControlStateNormal)];
        [_subMitBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
       
        _subMitBtn.backgroundColor = DefaultOrangeColor;
        _subMitBtn.layer.cornerRadius = 20;
        _subMitBtn.layer.masksToBounds = YES;
        _subMitBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        
        [_subMitBtn addTarget:self action:@selector(subMitAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _subMitBtn;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenNarrowSide, ScreenWideSide - self.tabBarController.tabBar.bounds.size.height - 15) style:(UITableViewStylePlain)];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellEditingStyleNone;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        
        if (@available(iOS 11.0, *)){
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _tableView.estimatedRowHeight           = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

@end
